//
//  WEBViewController.m
//  UI_豆瓣影院
//
//  Created by dllo on 16/1/9.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "WEBViewController.h"

@interface WEBViewController ()<UIWebViewDelegate>
@property (nonatomic, retain) UIWebView *webView;
@property (nonatomic, retain) UIView *view1;
@property (nonatomic, retain) UIActivityIndicatorView *activity;

@end

@implementation WEBViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"百度一下";
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"后退" style:UIBarButtonItemStyleDone target:self action:@selector(itemAction)];
    
    
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"前进" style:UIBarButtonItemStyleDone target:self action:@selector(item1Action)];
    
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"刷新" style:UIBarButtonItemStyleDone target:self action:@selector(item2Action)];
    
    UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(item3Action)];
    self.navigationItem.leftBarButtonItems = @[item3, item2];
    self.navigationItem.rightBarButtonItems = @[item1, item];
    
    [self initWebView];
    
}

//  小菊花后面的视图
- (void)viewView {
    self.view1 = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view1.backgroundColor = [UIColor lightGrayColor];
    self.view1.alpha = 0.5;
    [self.view addSubview:_view1];
    self.activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    self.activity.center = self.view.center;
    [self.view1 addSubview:self.activity];
    
    
}
- (void)item3Action {
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}


//  网友后退 前进 刷新
- (void)item2Action {
    
    [self.webView reload];
}


- (void)item1Action {
    
    [self.webView goForward];
    
}


- (void)itemAction {
    [self.webView goBack];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    
    NSLog(@"加载完成");
    [self.view1 removeFromSuperview];
    [self.activity stopAnimating];
    
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
    [self viewView];
    [self.activity startAnimating];
    NSLog(@"开始加载");
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    
    NSLog(@"加载出错");
}


- (void)initWebView {
    self.webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]]];
    });
    [self.view addSubview:_webView];
    _webView.delegate = self;
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
